/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package cz.ctu.android.DriveCommunicator.activities;

import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;

import cz.ctu.android.DriveCommunicator.R;
import cz.ctu.android.DriveCommunicator.utils.DriverConstants;
import cz.ctu.android.DriveCommunicator.utils.HandlerMessage;
import cz.ctu.android.DriveCommunicator.utils.ImageMapper;
import cz.ctu.android.DriveCommunicator.utils.Zprava;
import cz.ctu.android.adapters.CustomListViewAdapter;
import cz.ctu.android.entity.RowItem;

import android.app.Activity;
import android.content.Intent;
import android.gesture.Gesture;
import android.gesture.GestureLibraries;
import android.gesture.GestureLibrary;
import android.gesture.GestureOverlayView;
import android.gesture.GestureOverlayView.OnGesturePerformedListener;
import android.gesture.Prediction;
import android.os.Bundle;
import android.speech.tts.TextToSpeech;
import android.speech.tts.TextToSpeech.OnInitListener;
import android.view.Gravity;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.Button;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

/**
 * 
 * @author karlos
 */
public class MessageActivity extends Activity implements
		OnGesturePerformedListener, OnInitListener {

	int idToSent;
	private OnClickListener replyMessageButtonListener = new OnClickListener() {

		public void onClick(View v) {

			Bundle bundle = new Bundle();
			bundle.putInt("idToLoad", idToSent);
			Intent intent = new Intent(MessageActivity.this,
					ReplyKeyboardActivity.class);
			intent.putExtras(bundle);
			startActivity(intent);
		}
	};
	private OnClickListener deleteButtonListener = new OnClickListener() {

		public void onClick(View v) {
			HandlerMessage hl = new HandlerMessage();
			hl.deleteConversation(idToSent);
			Intent intent = new Intent(MessageActivity.this,
					MainActivity.class);
			startActivity(intent);
		}
	};
	

	private Button replyMessageButton;
	private String message = "Lorem Ipsum is simply dummy text.";
	private GestureLibrary mLibrary;
	private TextToSpeech tts;
	List<RowItem> rowItems;
	ListView listView;
	private int MY_DATA_CHECK_CODE = 0;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		Bundle bundle = this.getIntent().getExtras();
		idToSent = bundle.getInt("idToLoad");
		if (idToSent != DriverConstants.myId) {
			setContentView(R.layout.message);
			HandlerMessage hl = new HandlerMessage();
			ArrayList<Zprava> conversation = hl.getConversetion(idToSent);
			if (conversation != null) {
				rowItems = new ArrayList<RowItem>();
				for (int i = 0; i < conversation.size(); i++) {
					Zprava zprava = conversation.get(i);
					RowItem item = new RowItem(ImageMapper.getImage(zprava
							.getSenderID()), i+1,zprava.getDate(),
							zprava.getZprava());
					rowItems.add(item);
					if (i  == 0) {
						message = zprava.getZprava();
					}
				}

				listView = (ListView) findViewById(R.id.conversationList);
				CustomListViewAdapter adapter = new CustomListViewAdapter(this,
						R.layout.list_item, rowItems);
				listView.setAdapter(adapter);
			} else {
				Toast.makeText(MessageActivity.this,
						"Error conversation NULL for idLoad: " + idToSent,
						Toast.LENGTH_LONG).show();
			}
		}

		replyMessageButton = (Button) findViewById(R.id.replyMessageButton);
		replyMessageButton.setOnClickListener(replyMessageButtonListener);
		
		Button deleteButton = (Button) findViewById(R.id.delete);
		 deleteButton.setOnClickListener(deleteButtonListener);


		mLibrary = GestureLibraries.fromRawResource(this, R.raw.gestures);
		if (!mLibrary.load()) {
			finish();
		}

		GestureOverlayView gestures = (GestureOverlayView) findViewById(R.id.gestures);
		gestures.addOnGesturePerformedListener(this);

		Intent checkIntent = new Intent();
		checkIntent.setAction(TextToSpeech.Engine.ACTION_CHECK_TTS_DATA);
		startActivityForResult(checkIntent, MY_DATA_CHECK_CODE);
	}

	public void onGesturePerformed(GestureOverlayView overlay, Gesture gesture) {
		ArrayList<Prediction> predictions = mLibrary.recognize(gesture);

		if (predictions.size() > 0 && predictions.get(0).score > 1.0) {
			String result = predictions.get(0).name;

			if ("readSMS".equalsIgnoreCase(result)
					|| "readSMSreverse".equalsIgnoreCase(result)) {
				if (message != null && message.length() > 0) {
					tts.speak(message, TextToSpeech.QUEUE_ADD, null);
				}

			}
		}
	}

	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		if (requestCode == MY_DATA_CHECK_CODE) {
			if (resultCode == TextToSpeech.Engine.CHECK_VOICE_DATA_PASS) {
				// success, create the TTS instance
				tts = new TextToSpeech(this, this);
			} else {
				// missing data, install it
				Intent installIntent = new Intent();
				installIntent
						.setAction(TextToSpeech.Engine.ACTION_INSTALL_TTS_DATA);
				startActivity(installIntent);
			}
		}

	}

	public void onInit(int status) {
		if (status == TextToSpeech.ERROR) {
			Toast.makeText(MessageActivity.this,
					"Error occurred while initializing Text-To-Speech engine",
					Toast.LENGTH_LONG).show();
		}
	}

	// public OnItemClickListener onItemClick = new OnItemClickListener() {
	//
	// public void onItemClick(AdapterView<?> parent, View view, int position,
	// long id) {
	// Toast toast = Toast.makeText(getApplicationContext(), "Item "
	// + (position + 1) + ": " + rowItems.get(position),
	// Toast.LENGTH_SHORT);
	// toast.setGravity(Gravity.BOTTOM | Gravity.CENTER_HORIZONTAL, 0, 0);
	// toast.show();
	// Intent intent = new Intent(MessageActivity.this,
	// MessageActivity.class);
	// startActivity(intent);
	// }
	//
	// };

}
